package cn.teduymdj.jxc.controller;


import cn.teduymdj.jxc.myAnnotation.LogAnnotation;
import cn.teduymdj.jxc.pojo.dto.CustomerAddNewDTO;
import cn.teduymdj.jxc.pojo.dto.CustomerUpdateDTO;
import cn.teduymdj.jxc.pojo.entity.Log;
import cn.teduymdj.jxc.pojo.vo.CustomerListItemVO;
import cn.teduymdj.jxc.service.CustomerService;
import cn.teduymdj.jxc.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 客户Controller控制器
 *
 * @since 1.0
 * 刘校权
 */
@Validated
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    /**
     * 查询下拉框客户信息
     *
     * @return
     */
    @RequestMapping("/getComboboxList")
    public JsonResult<List<String>> getComboboxList() {
        List<String> comboboxList = customerService.getComboboxList();
        return JsonResult.ok(comboboxList);
    }

    /**
     * 分页查询客户
     *
     * @param page 当前页数
     * @param rows 每页显示的记录数
     * @param name 客户名
     * @return
     */
    //日志保存注解,不用管:李柯辰
    @LogAnnotation(content = "分页查询客户",type = Log.SELECT_ACTION)
    @RequestMapping("/list")
    public JsonResult<List<CustomerListItemVO>> list(Integer page, Integer rows, String name) {
        List<CustomerListItemVO> customers = customerService.getCustomerList(page, rows, name);
        return JsonResult.ok(customers);
    }

    /**
     * 添加客户
     *
     * @param customer 客户实体
     * @return
     */
    @LogAnnotation(content = "添加客户",type = Log.INSERT_ACTION)
    @RequestMapping("/save")
    public JsonResult<Void> save(CustomerAddNewDTO customer) {

        customerService.saveCustomer(customer);

        return JsonResult.ok();
    }
	/**
	 * 修改客户
	 *
	 * @param customer 客户实体
	 * @return
	 */
    @LogAnnotation(content = "修改客户",type = Log.UPDATE_ACTION)
    @RequestMapping("/update")
	public JsonResult<Void> update(CustomerUpdateDTO customer) {

		customerService.updateCustomer(customer);

		return JsonResult.ok();
	}

    /**
     * 删除客户
     *
     * @return
     */
    @LogAnnotation(content = "删除客户",type = Log.DELETE_ACTION)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        customerService.deleteCustomer(id);
        return JsonResult.ok();
    }

}
